//
//  LGLGNetWorkingManager.m
//  微博oc版
//
//  Created by Apple_Lzzy27 on 16/10/13.
//  Copyright © 2016年 Apple_Lzzy27. All rights reserved.
//

#import "LGNetWorkingManager.h"
@interface LGNetWorkingManager()

@end

@implementation LGNetWorkingManager



//创建一个单列
+ (instancetype)shard{
 
    static LGNetWorkingManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LGNetWorkingManager alloc] init];
//         [manager.responseSerializer addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"];
          //manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObject:@"application/json"];
        manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
//       
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        
    });
    
    
    return manager;
}

//因为每次请求都依赖于token，所以创建一个带token请求的方法
/**
 *  封装带token方法
 *
 *  @param method     请求的方法
 *  @param urlString  请求的地址
 *  @param parameters 请求的参数
 *  @param completion 请求结果回调
 */
-(void)tokenReques:(NetMethod)method urlString:(NSString *)urlString parameters:(NSMutableDictionary *)parameters completion:(void (^)(BOOL isSuccess,id responseObject))completion{
  
    if (parameters == nil) {
        parameters = [NSMutableDictionary dictionary];
    }
   
  

    [parameters setObject:@"2.00v4ddHC0ObsEH705c467dbaMQ__ME" forKey:@"access_token"];
   
   
    [self request:(NetMethod)method urlString:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(BOOL isSuccess,id responseObject))completion];
}

//
/**
 *  封装afn方法
 *
 *  @param method     请求的方法
 *  @param urlString  请求的地址
 *  @param parameters 请求的参数
 *  @param completion 请求结果回调
 */
- (void)request:(NetMethod)method urlString:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(BOOL isSuccess,id responseObject))completion{
    
  
        //成功回调
       void(^success)() = ^(NSURLSessionDataTask * _Nonnull task ,id  _Nullable responseObject) {
           completion(YES,responseObject);
        
    };
    //失败回调
    void(^failure)() = ^(NSURLSessionDataTask * _Nonnull task ,id  _Nullable responseObject) {
            NSLog(@"请求失败%@",task.response);
         completion(NO,nil);
    
    };

   
    
    if (method == GET) {
        [self GET:urlString parameters:parameters progress:nil success:success failure:failure];
    }else{
        
        NSLog(@"%@----%@",urlString,parameters);
        
        [[LGNetWorkingManager shard] POST:urlString parameters:parameters progress:nil success:success failure:failure];
      
    }
    
    
    
}
@end
